// pages/bought/bought.js
import deliveryApi from '../../service/deliveryApi'
import {times} from 'number-precision';
const app = getApp()
let codeConfig = {...app.globalData.codeConfig}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPupFlag: false, // 物流弹窗
    loadTips: '上拉加载',
    startPage: 1,
    pageSize: 15,
    loading: false,
    data: {
    },
    expressState: {...codeConfig.expressState},
    logisticsData: {},
    optionTalentId: '',
    deliveryId: '', // 提货记录ID
    upAbled: true,
    logisticsPic: '',
    logisticsName: '',
    tipMsg: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tipMsg: ''
    })
    if (options.tipMsg) {
      this.setData({
        tipMsg: '提货申请提交成功，请耐心等待发货！'
      })
    }
    this.getRecord()
  },
  // 已提货宝贝
  getRecord() {
    var that = this
    that.setData({
      loading: true,
      loadTips: '加载中...'
    })
    wx.showLoading({
      title: "加载中"
    })
    var params = {
      startPage: that.data.startPage,
      pageSize: that.data.pageSize
    }
    deliveryApi.recordDelivery(params).then(res => {
      wx.stopPullDownRefresh();
      wx.hideLoading()
      that.setData({
        loading: false
      })
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        var _data = res.data.data
        for (var i in _data.deliveryList) {
          if (_data.deliveryList[i].deliveryState === codeConfig.expressState.DELIVERING) {
            _data.deliveryList[i].statusText = '提货中'
          } else if (_data.deliveryList[i].deliveryState === codeConfig.expressState.WAIT_SEND) {
            _data.deliveryList[i].statusText = '等待发货'
          } else if (_data.deliveryList[i].deliveryState === codeConfig.expressState.SENT) {
            _data.deliveryList[i].statusText = '已发货'
          } else if (_data.deliveryList[i].deliveryState === codeConfig.expressState.RECEIVED) {
            _data.deliveryList[i].statusText = '已签收'
          }
        }
        that.setData({
          data: _data
        })
        if (res.data.data.deliveryList.length < that.data.pageSize) {
          that.setData({
            loadTips: '暂无更多数据',
            upAbled: false
          })
        } else {
          that.setData({
            loadTips: '上拉加载'
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    }).catch(e => {
      wx.stopPullDownRefresh();
      that.setData({
        loading: false
      });
    })
  },

  // 提醒发货
  onTipDeliverGoods () {
    wx.showToast({
      title: '已提醒发货，请耐心等待。',
      icon: 'none'
    })
  },

  // 物流弹窗
  showLogistics (e) {
    var that = this
    var deliveryId = e.target.dataset.deliveryid
    var goodsPic = e.target.dataset.goodspic
    var goodsName = e.target.dataset.goodsname
    this.setData({
      deliveryId,
      logisticsName: goodsName,
      logisticsPic: goodsPic
    })
    var params = {
      deliveryId: this.data.deliveryId
    }
    deliveryApi.expressDelivery(params).then(res => {
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        var _logisticsData = res.data.data
        _logisticsData.expressInfoList.push({content: _logisticsData.supplierName, time: ''})
        that.setData({
          showPupFlag : true,
          logisticsData: _logisticsData
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  closePup () {
    this.setData({
      showPupFlag : false
    })
  },
  // 复制订单号
  onCopy (e) {
    var orderId = e.currentTarget.dataset.orderid
    wx.setClipboardData({
      data: orderId
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    if (that.data.tipMsg !== '') {
      wx.showToast({
        title: that.data.tipMsg,
        icon: 'none'
      })
      that.setData({
        tipMsg: ''
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      startPage: 1,
      pageSize: 15,
      upAbled: true // 是否可上拉加载
    })
    this.getRecord()
  },

/**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.upAbled) {
      return false
    }
    if (that.data.loading) {
      return false
    }
    that.setData({
      loading: true,
      loadTips: '加载中...',
      startPage: that.data.startPage + 1
    })
    var params = {
      startPage: that.data.startPage,
      pageSize: that.data.pageSize
    }
    deliveryApi.recordDelivery(params).then(res => {
      that.setData({
        loading: false
      })
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        var _data = res.data.data
        for (var i in _data.deliveryList) {
          if (_data.deliveryList[i].deliveryState == 0) { 
            _data.deliveryList[i].statusText = '提货中' 
          } else if (_data.deliveryList[i].deliveryState == 1) {
            _data.deliveryList[i].statusText = '等待发货'
          } else if (_data.deliveryList[i].deliveryState == 2) {
            _data.deliveryList[i].statusText = '已发货'
          } else if (_data.deliveryList[i].deliveryState == 3) {
            _data.deliveryList[i].statusText = '提货完成'
          }
        }
        var arr = that.data.data.deliveryList.concat(_data.deliveryList)
        that.setData({
          'data.deliveryList': arr
        })
        if (res.data.data.deliveryList.length < that.data.pageSize) {
          that.setData({
            loadTips: '暂无更多数据',
            startPage: that.data.startPage - 1,
            upAbled: false
          })
        } else {
          that.setData({
            loadTips: '上拉加载'
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    }).catch(e => {
      console.log(e)
      that.setData({
        loading: false
      })
    })
  }
})